package com.example.k8s.hello.service.exam.service.impl;

import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.example.k8s.hello.api.sys.UserApi;
import com.example.k8s.hello.bean.exam.entity.EdExamDetailEntity;
import com.example.k8s.hello.bean.exam.entity.EdExamEntity;
import com.example.k8s.hello.bean.exam.form.EdExamSaveForm;
import com.example.k8s.hello.bean.exam.query.EdExamQuery;
import com.example.k8s.hello.bean.exam.query.EdUserQuery;
import com.example.k8s.hello.bean.exam.vo.EdExamVo;
import com.example.k8s.hello.bean.exam.vo.EdUserScoreVo;
import com.example.k8s.hello.bean.sys.query.SysUserKeyQuery;
import com.example.k8s.hello.common.bean.Page;
import com.example.k8s.hello.common.exception.ApiExEnum;
import com.example.k8s.hello.common.utils.StpExtendUtil;
import com.example.k8s.hello.service.exam.dao.EdExamDao;
import com.example.k8s.hello.service.exam.service.EdExamDetailService;
import com.example.k8s.hello.service.exam.service.EdExamService;
import lombok.RequiredArgsConstructor;
import org.apache.commons.lang3.StringUtils;
import org.springframework.context.annotation.Lazy;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.util.CollectionUtils;

import java.util.Arrays;
import java.util.List;

@RequiredArgsConstructor
@Service
public class EdExamServiceImpl extends ServiceImpl<EdExamDao, EdExamEntity> implements EdExamService {

    final UserApi userApi;
    final EdExamDetailService edExamDetailService;
    @Override
    public Page<EdExamEntity> queryPage(EdExamQuery query, Page page) {
        List<Long> userIds = null;
        String key = query.getKey();
        if (StringUtils.isNoneBlank(key)) {
//            userIds = sysUserDao.selectList(Wrappers.lambdaQuery(SysUserEntity.class)
//                            .select(SysUserEntity::getUserId)
//                            .like(SysUserEntity::getUsername, key)
//                            .or().like(SysUserEntity::getNickname, key))
//                    .stream().map(SysUserEntity::getUserId).toList();

            userIds = userApi.queryUserIds(new SysUserKeyQuery().setKey(query.getKey()));

            if (CollectionUtils.isEmpty(userIds)) {
                return page;
            }
        }

        return this.page(
                page,
                Wrappers.lambdaQuery(EdExamEntity.class)
                        // 非管理员只允许查看自己的成绩
//                        .eq(!StpExtendUtil.ifAdmin(), EdExamEntity::getUserId, StpExtendUtil.getUserId())
                        .in(!CollectionUtils.isEmpty(userIds), EdExamEntity::getUserId, userIds)
                        .eq(query.getUserId() != null, EdExamEntity::getUserId, query.getUserId())
                        .eq(query.getScene() != null, EdExamEntity::getScene, query.getScene())
                        .eq(query.getSubject() != null, EdExamEntity::getSubject, query.getSubject())
                        .orderByDesc(EdExamEntity::getCreateTime));
    }

    @Override
    public Page<EdUserScoreVo> queryUserScorePage(EdUserQuery query, Page page) {
        return baseMapper.queryUserScorePage(query, page);
    }

    @Override
    public EdExamVo getVoById(Long examId) {
        EdExamEntity examEntity = getById(examId);
        ApiExEnum.A0010.assertTrue(examEntity != null, "该数据不存在");

        if (!StpExtendUtil.ifAdmin()) {
            ApiExEnum.A0006.assertTrue(StpExtendUtil.getUserId().equals(examEntity.getUserId())
                    , "非管理员只允许查看自己的考试成绩");
        }

        List<EdExamDetailEntity> details = edExamDetailService.list(Wrappers.lambdaQuery(EdExamDetailEntity.class).eq(EdExamDetailEntity::getExamId, examId));

        return EdExamVo.build(examEntity, details);
    }

    @Override
    @Transactional
    public EdExamVo saveExam(EdExamSaveForm form) {
        EdExamEntity examEntity = form.buildExam();

        save(examEntity);

        edExamDetailService.saveBatch(form.buildDetails(examEntity.getExamId()));

        return getVoById(examEntity.getExamId());
    }

    @Override
    public void deleteBatch(Long[] examIds) {
        if (examIds.length < 1) {
            return;
        }

        removeBatchByIds(Arrays.asList(examIds));

        edExamDetailService.remove(Wrappers.lambdaQuery(EdExamDetailEntity.class).in(EdExamDetailEntity::getExamId, examIds));
    }
}
